/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2015-2020. All rights reserved.
 */

package org.example;

import com.huaweicloud.sdk.as.v1.AsClient;
import com.huaweicloud.sdk.as.v1.model.PauseScalingGroupOption;
import com.huaweicloud.sdk.as.v1.model.PauseScalingGroupRequest;
import com.huaweicloud.sdk.as.v1.model.PauseScalingGroupResponse;
import com.huaweicloud.sdk.as.v1.region.AsRegion;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;

public class PauseScalingGroupSolution {

    public static void main(String[] args) {
        ICredential auth = initCredential();

        AsClient client = initAsClient(auth);

        PauseScalingGroupRequest request = initPauseScalingGroupRequest();
        try {
            PauseScalingGroupResponse response = client.pauseScalingGroup(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            // log error stack
        } catch (RequestTimeoutException e) {
            // log error stack
        } catch (ServiceResponseException e) {
            // log error stack
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static PauseScalingGroupRequest initPauseScalingGroupRequest() {
        PauseScalingGroupRequest request = new PauseScalingGroupRequest();
        PauseScalingGroupOption body = initPauseScalingGroupOption();
        request.withBody(body);
        request.withScalingGroupId("${scaling group id}");
        return request;
    }

    private static PauseScalingGroupOption initPauseScalingGroupOption() {
        PauseScalingGroupOption pauseScalingGroupOption = new PauseScalingGroupOption();
        pauseScalingGroupOption.withAction(PauseScalingGroupOption.ActionEnum.PAUSE);
        return pauseScalingGroupOption;
    }

    private static AsClient initAsClient(ICredential auth) {
        return AsClient.newBuilder()
            .withCredential(auth)
            .withRegion(AsRegion.valueOf("${region id}"))
            .build();
    }

    private static ICredential initCredential() {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        return new BasicCredentials()
            .withAk(ak)
            .withSk(sk);
    }
}
